package com.yykj.drup.server.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yykj.drup.server.dto.PrescriptionProductDto;
import com.yykj.drup.server.form.WestDrug;
import com.yykj.drup.server.mapper.ProductDao;
import com.yykj.drup.server.model.Category;
import com.yykj.drup.server.model.Product;
import com.yykj.drup.server.model.StoreProduct;
import com.yykj.drup.server.param.ProductNameParam;
import com.yykj.drup.server.param.ProductParam;
import com.yykj.drup.server.service.CategoryService;
import com.yykj.drup.server.service.ProductService;
import com.yykj.drup.server.service.StoreProductService;
import com.yykj.prescription.client.dto.PrescriptionDetailDTO;
import com.yykj.prescription.client.dto.PrescriptionDrugChinaDTO;
import com.yykj.prescription.client.dto.PrescriptionDrugWestDTO;
import com.yykj.prescription.client.enums.PrescriptionType;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zengg
 * @since 2022-03-09
 */
@Service
@AllArgsConstructor
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductService {

    final ProductDao productDao;
    final StoreProductService storeProductService;
    final CategoryService categoryService;

    @Override
    public  IPage<Product> findProductPageByParam(ProductParam param, IPage page) {
        List<Integer> cids = new ArrayList<>();
        if (param.getCategoryId() == 0){
            cids.add(param.getPentCategoryId());
            //获取一级分类下子分类列表
            List<Category> categories = categoryService.getCategoryByParentId(param.getPentCategoryId());
            if (!categories.isEmpty()){
                List<Integer> list = categories.stream().map(Category::getId).collect(Collectors.toList());
                cids.addAll(list);
            }
        }
        IPage iPage =
                this.page(page, new QueryWrapper<Product>()
                                        .in(!cids.isEmpty(),"category_id",cids)
                                        .eq(param.getCategoryId() != 0,"category_id" , param.getCategoryId())
                                        );
        return iPage;

    }

    @Override
    public IPage<Product> findProductPageByNameParam(ProductNameParam param, IPage page) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper
                .eq("category_type",param.getCategoryType())
                .and(StrUtil.isNotEmpty(param.getNameAll()),
                        i -> i.like("title",param.getNameAll()).or()
                                .like("py", param.getNameAll()).or()
                                .like("py_all",param.getNameAll()));
        return this.page(page, wrapper);
    }

    @Override
    public PrescriptionProductDto findProductDtById(Integer productId) {
        return productDao.findProductDtById(productId);
    }

    @Override
    public List<Product> findStoreMissingProduct(Integer storeId, List<Integer> products) {
        List<Product> productsList = new ArrayList<>();
        //定义获取的药品id集合
        List<Integer> neetIds;
        List<StoreProduct> storeProducts = storeProductService.findStoreProductByStoreId(storeId);
        if (!storeProducts.isEmpty()){
            //获取药店含有的药品列表
            List<Integer> haveProducts = new ArrayList<>();
            for (StoreProduct storeProduct : storeProducts) {
                Integer productId = storeProduct.getProductId();
                haveProducts.add(productId);
            }
            //比对获取差集的药品信息
            neetIds = products.stream().filter(item -> !haveProducts.contains(item)).collect(Collectors.toList());
            if (!neetIds.isEmpty()){
                productsList = this.findStoreMissingProduct(neetIds);
            }
        }else {
            neetIds  = products;
            productsList = this.findStoreMissingProduct(neetIds);
        }
        return productsList;
    }

    @Override
    public List<Product> findStoreMissingProduct(List<Integer> productIds) {

        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.in("id",productIds);
        return this.list(wrapper);
    }

    @Override
    public List<WestDrug> findWestDrugs(PrescriptionDetailDTO detailDTO) {
        List<WestDrug> westDrugs = new ArrayList<>();
        log.info("处方药品类别：{}",detailDTO.getType());
        log.info("PrescriptionType.west：{}",PrescriptionType.west);
        System.out.println(detailDTO.getType().equals(PrescriptionType.west));
        if (detailDTO.getType().equals(PrescriptionType.west)){
            //整理西药
            List<PrescriptionDrugWestDTO> westDrugList = detailDTO.getWestDrugList();
            westDrugs = westDrugList.stream().map(i -> new WestDrug(i.getPrescriptionId(), Integer.parseInt(i.getDrugId()),i.getDrugCommonName() ,i.getSpec(), i.getCnt(), null)).collect(Collectors.toList());
        }else {
            //整理中药
            List<PrescriptionDrugChinaDTO> chinaDrugList = detailDTO.getChinaDrugList();
            westDrugs = chinaDrugList.stream().map(i -> new WestDrug(i.getPrescriptionId(), Integer.parseInt(i.getDrugId()), i.getName() ,null, Integer.parseInt(i.getGrams()), i.getNote())).collect(Collectors.toList());

        }
        return westDrugs;
    }
}
